﻿Imports TMySQL
Public Class frmProcedure
    Implements ITabForm

    Private _routine As IMyRoutine
    Public Sub New(ByVal ARoutine As IMyRoutine)
        Me.New()
        _routine = ARoutine
        _routine.Arguments.Refresh()
        InitFrm()
    End Sub
    Private Sub New()
        ' この呼び出しは、Windows フォーム デザイナで必要です。
        InitializeComponent()
        ' InitializeComponent() 呼び出しの後で初期化を追加します。
    End Sub
    Private Sub InitFrm()
        txtRoutineSchema.Text = _routine.RoutineSchema
        txtRoutineName.Text = _routine.RoutineName
        txtRoutineComment.Text = _routine.RoutineComment
        txtDefiner.Text = _routine.Definer
        txtSource.Text = _routine.SourceText

        InitArguments()
        InitExecArguments()
        Common.SetGridAllowSorting(Grid, DataGridViewColumnSortMode.NotSortable)
        Common.SetGridAllowSorting(GridParam, DataGridViewColumnSortMode.NotSortable)
        frmMain.GeneralSettings.ApplyAll(Me)
        frmMain.EditorSettings.ApplyAll(Me)
        frmMain.GridSettings.ApplyAll(Me)
    End Sub

    Private Sub InitExecArguments()
        Grid.Columns.Clear()
        Dim col As New DataGridViewTextBoxColumn
        col.HeaderText = "名前"
        col.ReadOnly = True
        col.Width = 60
        Grid.Columns.Add(col)

        col = New DataGridViewTextBoxColumn
        col.HeaderText = "型"
        col.ReadOnly = True
        col.Width = 60
        Grid.Columns.Add(col)

        col = New DataGridViewTextBoxColumn
        col.HeaderText = "長さ"
        col.ReadOnly = True
        col.Width = 60
        Grid.Columns.Add(col)

        col = New DataGridViewTextBoxColumn
        col.HeaderText = "向き"
        col.ReadOnly = True
        col.Width = 60
        Grid.Columns.Add(col)

        col = New DataGridViewTextBoxColumn
        col.HeaderText = "値　　　　　　　"
        col.ReadOnly = False
        col.Width = 100
        Grid.Columns.Add(col)

        Grid.Rows.Clear()
        _routine.Arguments.Refresh()
        For i As Integer = 0 To _routine.Arguments.Count - 1
            If _routine.Arguments(i).ParameterMode <> "RETURNS" Then
                Grid.Rows.Add(_routine.Arguments(i).ParameterName, _routine.Arguments(i).DataType, _routine.Arguments(i).ArgLength & "." & _routine.Arguments(i).NumericScale, _routine.Arguments(i).ParameterMode, String.Empty)
            End If
        Next
    End Sub
    Private Sub InitArguments()

        GridParam.Columns.Clear()
        Dim col As New DataGridViewTextBoxColumn
        col.HeaderText = "名前"
        col.ReadOnly = True
        col.Width = 60
        GridParam.Columns.Add(col)

        col = New DataGridViewTextBoxColumn
        col.HeaderText = "型"
        col.ReadOnly = True
        col.Width = 60
        GridParam.Columns.Add(col)

        col = New DataGridViewTextBoxColumn
        col.HeaderText = "長さ"
        col.ReadOnly = True
        col.Width = 60
        GridParam.Columns.Add(col)

        col = New DataGridViewTextBoxColumn
        col.HeaderText = "向き"
        col.ReadOnly = True
        col.Width = 60
        GridParam.Columns.Add(col)

        GridParam.Rows.Clear()
        _routine.Arguments.Refresh()
        For i As Integer = 0 To _routine.Arguments.Count - 1
            GridParam.Rows.Add(_routine.Arguments(i).ParameterName, _routine.Arguments(i).DataType, _routine.Arguments(i).ArgLength & "." & _routine.Arguments(i).NumericScale, _routine.Arguments(i).ParameterMode)
        Next
    End Sub

    Private Sub cmdExecute_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdExecute.Click
        Cursor.Current = Cursors.WaitCursor
        Try
            SetArgsValue()
            ExecuteStoredProc()
        Catch ex As Exception
            Call MsgBox("エラーが発生しました" & vbNewLine & ex.Message, MsgBoxStyle.Exclamation)
        Finally
            Cursor.Current = Cursors.Default
        End Try
    End Sub
    Private Sub SetArgsValue()
        Dim pos As Integer = 0
        For i As Integer = 0 To _routine.Arguments.Count - 1
            If _routine.Arguments(i).ParameterMode = "IN" OrElse _routine.Arguments(i).ParameterMode = "INOUT" Then
                If Not Grid.Item(4, pos).Value Is Nothing Then
                    _routine.Arguments(i).Value = Grid.Item(4, pos).Value
                End If
            End If
            If _routine.Arguments(i).ParameterMode <> "RETURNS" Then
                pos += 1
            End If
        Next
    End Sub
    Private Sub ExecuteStoredProc()
        For i As Integer = 0 To _routine.Arguments.Count - 1
            If _routine.Arguments(i).ParameterMode = "IN" OrElse _routine.Arguments(i).ParameterMode = "INOUT" Then
                txtResult.Text &= _routine.Arguments(i).ParameterName & " : " & _routine.Arguments(i).Value.ToString & vbNewLine
            End If
        Next
        txtResult.Text &= _routine.SQLText & vbNewLine
        _routine.Execute()
        txtResult.Text &= vbNewLine & "==============================================" & vbNewLine
        For i As Integer = 0 To _routine.Arguments.Count - 1
            If _routine.Arguments(i).ParameterMode <> "RETURNS" Then
                txtResult.Text &= _routine.Arguments(i).ParameterName & " : " & _routine.Arguments(i).Value.ToString & vbNewLine
            Else
                txtResult.Text &= "RETURNS : " & _routine.Arguments.ReturnArg.Value.ToString & vbNewLine
            End If
        Next
        txtResult.Text &= vbNewLine
    End Sub
End Class